let app = getApp();
var util = require("../../utils/util.js");
var siteinfo = require('../../siteinfo.js');
Page({

    data: {
        idNumber: '',
        img: '/image/photo.png',
        region: '',
        switch1Checked: false,
        text: '已隐身',
        workImg: [],//工作照片
        imglist: [],
        headPortrait: '',
        resume: {
            educationExperience: '',
            workExperience: '',
            project: '',
            certificate: '',
            age: ''
        },
        sexList: [{
            id: 0,
            name: '请选择性别'
        }, {
            id: 1,
            name: '男'
        }, {
            id: 2,
            name: '女'
        }],
        sex: 0,
        statuList: [{
            id: 0,
            name: '请选择工作状态'
        }, {
            id: 1,
            name: '待岗'
        }, {
            id: 2,
            name: '上户中'
        }],
        applyState: 0,
        marriageList: [{
            id: 0,
            name: '请选择婚姻状况'
        }, {
            id: 1,
            name: '已婚'
        }, {
            id: 2,
            name: '未婚'
        }],
        marriage: 0,
        educationList: ["请选择学历", '博士', '硕士', '研究生', '本科', '大专', '高中', '初中', '小学'], //学历
        indexEducation: 0,
        workYearsList: ["请选择工作年限", '1年', '2年', '3年', '4年', '5年', '6年', '7年', '8年', '9年', '10年', '10年以上'],
        workYears: 0,
        nationList: ["请选择民族", '汉族', '蒙古族', '回族', '藏族', '维吾尔族', '苗族', '彝族', '壮族', '布依族', '朝鲜族', '满族', '侗族', '瑶族', '白族', '土家族', '其他'], //民族
        region: ["", "", ""],
        indexNation: 0,
        jobNatureList: [{
            id: 0,
            name: '请选择住家情况'
        }, {
            id: 1,
            name: '住家'
        }, {
            id: 2,
            name: '不住家'
        }],
        jobNatureIndex: 0,
        isPushList: [{
            id: '',
            name: '请选择是否推送'
        }, {
            id: 0,
            name: '否'
        }, {
            id: 1,
            name: '是'
        }],
        isPushIndex: 0,
        age: '',
        arr: [],
        items: [
            { value: '育婴师', checked: '' },
            { value: '月嫂', checked: '' },
            { value: '催乳师', checked: '' },
            { value: '育儿嫂', checked: '' },
            { value: '养老护理', checked: '' },
            { value: '中西面点', checked: '' },
            { value: '收纳师', checked: '' }
        ],
    },
    onLoad: function (options) {
        var _this = this
        _this.getResume()
        var time = util.formatTime(new Date());
        _this.setData({
            nowDate: time.substring(0, 10),
        })
    },

    onShow: function () {

    },
    //删除教育经历
    deleteEducation: function (e) {
        var _this = this
        var idx = e.currentTarget.dataset.idx;
        var educationExperience = _this.data.resume.educationExperience;
        educationExperience.splice(idx, 1)
        var a = 'resume.educationExperience'
        _this.setData({
            [a]: educationExperience
        })
    },
    //删除工作经历
    deleteWork: function (e) {
        var _this = this
        var idx = e.currentTarget.dataset.idx;
        var workExperience = _this.data.resume.workExperience;
        workExperience.splice(idx, 1)
        var a = 'resume.workExperience'
        _this.setData({
            [a]: workExperience
        })
    },
    //删除项目经历
    deleteProject: function (e) {
        var _this = this
        var idx = e.currentTarget.dataset.idx;
        var project = _this.data.resume.project;
        project.splice(idx, 1)
        var a = 'resume.project'
        _this.setData({
            [a]: project
        })
    },
    //删除证书
    deleteCertificate: function (e) {
        var _this = this
        var idx = e.currentTarget.dataset.idx;
        var certificate = _this.data.resume.certificate;
        certificate.splice(idx, 1)
        var a = 'resume.certificate'
        _this.setData({
            [a]: certificate
        })
    },
    bindRegionChange: function (e) {
        this.setData({
            region: e.detail.value
        })
    },
    isPushChange: function (e) {
        var _this = this;
        this.setData({
            marriage: e.detail.value,
        })
    },
    monthlyChange: function (e) {
        var _this = this;
        _this.setData({
            monthlyIndex: e.detail.value,
        })
    },
    statuChange: function (e) {
        var _this = this;
        _this.setData({
            applyState: e.detail.value,
        })
    },
    nativeChage: function (e) {
        var _this = this;
        this.setData({
            nativePlace: e.detail.value,
        })
    },
    workYearsChange: function (e) {
        var _this = this;
        _this.setData({
            workYears: e.detail.value,
        })
    },

    graduatedChange: function (e) {
        var _this = this;
        this.setData({
            graduatedFrom: e.detail.value,
        })
    },
    skillChage: function (e) {
        var _this = this;
        this.setData({
            skills: e.detail.value,
        })
    },
    workChage: function (e) {
        var _this = this;
        this.setData({
            workYears: e.detail.value,
        })
    },
    addressChage: function (e) {
        var _this = this;
        this.setData({
            liveAddress: e.detail.value,
        })
    },
    selfChage: function (e) {
        var _this = this;
        this.setData({
            selfEvaluation: e.detail.value,
        })
    },
    bindDateChange: function (e) {
        var _this = this;
        _this.data.resume.age = e.detail.value
        this.setData({
            resume: _this.data.resume
        })
    },

    nameChagen: function (e) {
        var _this = this;
        this.setData({
            name: e.detail.value,
        })
    },
    //选择性别
    sexChange: function (e) {
        var _this = this;
        _this.setData({
            sex: e.detail.value,
            // sex: _this.data.sexList[e.detail.value].id
        })
    },
    checkLabs(e) {
        var that = this,
            index = e.currentTarget.dataset.index,
            value = e.currentTarget.dataset.value,
            items = that.data.items,
            arr = that.data.arr,
            val = items[index].checked, //点击前的值
            limitNum = 7 - this.data.num,
            curNum = 0; //已选择数量

        //选中累加
        for (var i in items) {
            if (items[i].checked) {
                curNum += 1;
            }
            that.setData({
                curr: curNum + 1
            })
        }
        if (!val) {
            if (curNum == limitNum) {
                wx.showModal({
                    content: '选择数量不能超过' + (limitNum) + '个',
                    showCancel: false
                })
                return;
            }
            arr.push(value);
        } else {
            for (var i in arr) {
                if (arr[i] == value) {
                    arr.splice(i, 1);
                }
            }
        }
        items[index].checked = !val;
        that.setData({
            items: items,
            arr: arr
        })
    },
    //选择学历
    educationChange: function (e) {
        this.setData({
            indexEducation: e.detail.value
        })
    },
    workYearChange: function (e) {
        this.setData({
            workYears: e.detail.value
        })
    },
    //选择民族
    nationChange: function (e) {
        this.setData({
            indexNation: e.detail.value
        })
    },
    //选择工作性质
    jobNatureChange: function (e) {
        var _this = this;
        this.setData({
            jobNatureIndex: e.detail.value,
        })
    },
    /**
     * 获取简历信息
     */

    getResume: function () {
        var _this = this
        app._getLogin(siteinfo.selResume, {}, function (result) {
            if (result.data.province != '' && result.data.province != null) {
                var a = 'region[' + 0 + ']'
                _this.setData({ [a]: result.data.province });
            }
            if (result.data.city != '' && result.data.city != null) {
                var b = 'region[' + 1 + ']'
                _this.setData({ [b]: result.data.city });
            }
            if (result.data.district != '' && result.data.district != null) {
                var c = 'region[' + 2 + ']'
                _this.setData({ [c]: result.data.district });
            }
            if (result.data.educationExperience != null)
                result.data.educationExperience = JSON.parse(result.data.educationExperience)
            if (result.data.workExperience != null)
                result.data.workExperience = JSON.parse(result.data.workExperience)
            if (result.data.project != null)
                result.data.project = JSON.parse(result.data.project)
            if (result.data.certificate != null)
                result.data.certificate = JSON.parse(result.data.certificate)
            if (result.data.workImg != null)
                _this.setData({
                    workImg: JSON.parse(result.data.workImg)
                })
            if (result.data.imglist != null)
                _this.setData({
                    imglist: JSON.parse(result.data.imglist)
                })

            _this.setData({
                sex: parseInt(result.data.sex)
            })
            for (var i = 0; i < _this.data.educationList.length; i++) {
                if (result.data.education == _this.data.educationList[i]) {
                    _this.setData({
                        indexEducation: i
                    })
                    break
                }
            }
            for (var i = 0; i < _this.data.nationList.length; i++) {
                if (result.data.nation == _this.data.nationList[i]) {
                    _this.setData({
                        indexNation: i
                    })
                    break
                }
            }
            for (var i = 0; i < _this.data.workYearsList.length; i++) {
                if (result.data.workYears == _this.data.workYearsList[i]) {
                    _this.setData({
                        workYears: i
                    })
                    break
                }
            }
            //读取用户已保存的类型数据
            _this.setData({
                arr: result.data.arr.split(',')
            })
            for (var i = 0; i < _this.data.arr.length; i++) {
                for (var j = 0; j < _this.data.items.length; j++) {
                    if (_this.data.items[j].value == _this.data.arr[i]) {
                        _this.data.items[j].checked = true;
                        break;
                    }
                }
            }
            //使用setData更新数据
            _this.setData({
                items: _this.data.items
            })
            for (var i = 0; i < _this.data.marriageList.length; i++) {
                if (result.data.marriage == _this.data.marriageList[i].id) {
                    _this.setData({
                        marriage: i
                    })
                    break
                }
            }
            for (var i = 0; i < _this.data.statuList.length; i++) {
                if (result.data.applyState == _this.data.statuList[i].id) {
                    _this.setData({
                        applyState: i
                    })
                    break
                }
            }
            for (var i = 0; i < _this.data.jobNatureList.length; i++) {
                if (result.data.jobNatureIndex == _this.data.jobNatureList[i].id) {
                    _this.setData({
                        jobNatureIndex: i
                    })
                    break
                }
            }
            _this.setData({
                resume: result.data,
                headPortrait: result.data.headPortrait
            });
            _this.setData({
                age: util.getAge(result.data.age)
            })
        });

    },

    idNumberChagen: function (e) {
        var _this = this;
        console.log(e,'asdfa');
        this.setData({
            idNumber: e.detail.value,
        })
    },

    // 表单提交
    formSubmit: function (e) {
        var _this = this,
            values = e.detail.value;
        values.jobNatureIndex = _this.data.jobNatureIndex;
        values.education = _this.data.educationList[_this.data.indexEducation] //学历
        values.nation = _this.data.nationList[_this.data.indexNation] //民族
        values.workYears = _this.data.workYearsList[_this.data.workYears]
        values.age = _this.data.resume.age //出生日期
        if (!_this.validation(values)) {
            app.showError(_this.data.error);
            return false;
        }
        app._post_form(siteinfo.updResume, {
            idNumber: _this.data.idNumber,
            arr: _this.data.arr,//类型
            sex: _this.data.sexList[_this.data.sex].id,
            applyState: _this.data.statuList[_this.data.applyState].id,
            marriage: _this.data.marriageList[_this.data.marriage].id,
            jobNatureIndex: _this.data.jobNatureList[_this.data.jobNatureIndex].id,
            nation: values.nation,
            name: values.name,
            headPortrait: _this.data.headPortrait,
            education: values.education,
            workYears: values.workYears,//工作年限
            nativePlace: values.nativePlace,//籍贯
            // jobNatureIndex:values.jobNatureIndex,//住家情况
            liveAddress: values.liveAddress,//详细地址
            skills: values.skills, //专业技能
            age: values.age,
            selfEvaluation: values.selfEvaluation,//自我评价
            // marriage: _this.data.marriageList[_this.data.marriage].id, //婚姻状况
            jobNature: _this.data.jobNatureList[_this.data.jobNatureIndex].id, //住家情况
            workImg: JSON.stringify(_this.data.workImg),//工作照片
            imglist: JSON.stringify(_this.data.imglist),//荣誉证书,
            province: _this.data.region[0],
            city: _this.data.region[1],
            district: _this.data.region[2],
            workExperience: JSON.stringify(_this.data.resume.workExperience), //工作经验
            educationExperience: JSON.stringify(_this.data.resume.educationExperience), //学习经历
        },
            function (res) {
                wx.showToast({
                    title: '保存成功'
                })
                wx.navigateTo({
                    url: "/pages/myResume/myResume"
                });
            });
    },
    /**
    * 表单验证
    */
    validation: function (values) {
        var _this = this
        if (_this.data.arr == '') {
            _this.data.error = '请选择类型'
            return false
        }
        if (values.name == '') {

            _this.data.error = '姓名不能为空'
            return false
        }
        if(!_this.data.idNumber){
            _this.data.error = '身份证号不能为空'
            return false
        }
        if (0 == values.sex) {
            console.log(values);
            console.log('values');
            this.data.error = '性别不能为空';
            return false;
        }
        if ('请选择工作年限' == values.workYears) {
            this.data.error = '工作年限不能为空';
            return false;
        }
        if (0 == values.applyState) {
            this.data.error = '工作状态不能为空';
            return false;
        }
        if ('' == values.age || null == values.age || values.age == '-1') {
            this.data.error = '请选择出生日期';
            return false;
        }
        if ('请选择民族' == values.nation) {
            this.data.error = '民族不能为空';
            return false;
        }
        if ('请选择学历' == values.education) {
            this.data.error = '学历不能为空';
            return false;
        }
        if ('' == values.skills) {
            this.data.error = '专业不能为空';
            return false;
        }
        if (0 == values.marriage) {
            this.data.error = '婚姻状况不能为空';
            return false;
        }
        if ('' == values.nativePlace) {
            this.data.error = '籍贯不能为空';
            return false;
        }
        if (0 == values.jobNatureIndex) {
            this.data.error = '请选择住家情况';
            return false;
        }

        if ('' == _this.data.region[0]) {
            this.data.error = '居住省市区不能为空';
            return false;
        }
        if ('' == values.liveAddress) {
            this.data.error = '现居住详细地址不能为空';
            return false;
        }
        if ('' == values.selfEvaluation) {
            this.data.error = '自我评价不能为空';
            return false;
        }

        return true;
    },
    formReset: function (e) {
        var _this = this
        _this.setData({
            index: 0,
            indexCer: 0
        })
    },

    //新增工作经历
    toWork: function (e) {
        var _this = this
        var eindex = e.currentTarget.dataset.idx
        // if(_this.data.resume.workExperience==''){
        if (eindex == -1) {
            if (_this.data.resume.workExperience == '') {
                let arr = []
                wx.navigateTo({
                    url: '/pages/userWork/userWork?workExperience=' + JSON.stringify(arr),
                })
            } else {
                wx.navigateTo({
                    url: '/pages/userWork/userWork?workExperience=' + JSON.stringify(_this.data.resume.workExperience),
                })
            }

        } else {
            if (_this.data.resume.workExperience == '') {
                let arr = []
                wx.navigateTo({
                    url: '/pages/userWork/userWork?workExperience=' + JSON.stringify(arr) + '&id=' + e,
                })
            } else {
                wx.navigateTo({
                    url: '/pages/userWork/userWork?workExperience=' + JSON.stringify(_this.data.resume.workExperience) + '&id=' + e + '&datalist=' + e,
                })
            }

        }
    },
    //s上传照片
    chooseImage(e) {
        var _this = this
        wx.chooseImage({
            sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
            sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
            success: res => {
                if (res.tempFiles[0].size < 1048576) {
                    wx.getImageInfo({
                        src: res.tempFilePaths[0],
                        success(result) {
                            const tempFilePaths = res.tempFilePaths
                            app._uploadFile(siteinfo.fileImprot,
                                res.tempFiles[0].path,
                                'file', {
                                name: res.tempFiles[0].name
                            },
                                function (res1) {
                                    _this.setData({
                                        headPortrait: res1.data,
                                        img: tempFilePaths,
                                    })
                                })
                        }
                    })
                } else {
                    app.showError('寸照应小于1M！')
                }
            }
        })
    },
    chooseImg: function (e) {
        var that = this;
        wx.chooseImage({
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: function (res) {
                var workImg = that.data.workImg
                var tempFiles = res.tempFiles
                for (var i = 0; i < res.tempFilePaths.length; i++) {
                    if (workImg.length >= 9) {
                        that.setData({
                            workImg: workImg
                        });
                        return false;
                    } else {
                        wx.getImageInfo({
                            src: res.tempFilePaths[i],
                            success(result) {
                                app._uploadFile(siteinfo.fileImprot,
                                    result.path,
                                    'file', {
                                    name: result.name
                                },
                                    function (res1) {
                                        workImg.push(res1.data);
                                        that.setData({
                                            workImg: workImg
                                        })
                                    })
                            }
                        })
                    }
                }
            }

        });
    },
    // 删除图片
    deleteImg: function (e) {
        var workImg = this.data.workImg;
        var index = e.currentTarget.dataset.index;
        workImg.splice(index, 1);
        this.setData({
            workImg: workImg
        });
    },
    // 预览图片
    previewImg: function (e) {
        var index = e.currentTarget.dataset.index;
        var workImg = this.data.workImg;
        wx.previewImage({
            current: workImg[index],
            urls: workImg
        })
    },
    chooseImgs: function (e) {
        var that = this;
        wx.chooseImage({
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: function (res) {
                var imglist = that.data.imglist
                var tempFiles = res.tempFiles
                for (var i = 0; i < res.tempFilePaths.length; i++) {
                    if (imglist.length >= 9) {
                        that.setData({
                            imglist: imglist
                        });
                        return false;
                    } else {
                        wx.getImageInfo({
                            src: res.tempFilePaths[i],
                            success(result) {
                                app._uploadFile(siteinfo.fileImprot,
                                    result.path,
                                    'file', {
                                    name: result.name
                                },
                                    function (res1) {
                                        imglist.push(res1.data);
                                        that.setData({
                                            imglist: imglist
                                        })
                                    })
                            }
                        })
                    }
                }
            }
        });
    },
    // 删除图片
    deleteImgs: function (e) {
        var imglist = this.data.imglist;
        var index = e.currentTarget.dataset.index;
        imglist.splice(index, 1);
        this.setData({
            imglist: imglist
        });
    },
    // 预览图片
    previewImgs: function (e) {
        var index = e.currentTarget.dataset.index;
        var imglist = this.data.imglist;
        wx.previewImage({
            current: imglist[index],
            urls: imglist
        })
    },
    onShareAppMessage: function () {

    },

})